<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/8
 * Time: 9:42
 */

namespace app\shawting\controller;

use think\Controller;
use think\Session;

class Login extends Controller
{

    public function index()
    {
        if(request()->isPost()){
            $username = input("username");
            $password = input("password");
            if(empty($username) || empty($password)){
                $this->error("账号和密码不能为空！");
            }
            $username = db("city_admin")->where(['username'=>$username])->find();
            if($username){
                if(md5($password) === $username['password']){
                    Session::set("admin_name","管理员");
                    Session::set("admin_id","1");
                    $this->success("登入成功","index/index");
                }else{
                    $this->error("密码错误");
                }
            }else{
                $this->error("账号错误");
            }

        }
        return $this->fetch();
    }

    public function outLogin(){
        Session::delete('admin_name');
        Session::delete('admin_id');
        $this->redirect("login/index");
    }

}